<%@page import="com.ckerchina.admin.ckadmin.consts.ActorType"%>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ include file="/tags.jsp"%>
<aos:html>
<aos:head title="用户菜单">
	<aos:include lib="ext" />
	<aos:base href="console" />
</aos:head>
<aos:body>
</aos:body>
<aos:onready>
	<aos:viewport layout="border">
		<aos:gridpanel id="_g_index" url="findAllMgrUser" region="west"
			bodyBorder="0 1 0 0" hidePagebar="true" width="900"
			onrender="_g_index_query" onitemclick="itemclick" forceFit="true"
			title="用户">
			<aos:docked forceBoder="0 1 1 0">
				<aos:dockeditem text="新增" onclick="_w_index_show" icon="add.png" />
				<aos:dockeditem text="修改" onclick="_w_index_u_show" icon="edit.png" />
				<aos:dockeditem text="删除" onclick="_g_index_del" icon="del.png" />
				<aos:dockeditem xtype="tbfill" />
				<aos:triggerfield emptyText="用户名" id="_key_name_"
					onenterkey="_g_index_query" trigger1Cls="x-form-search-trigger"
					onTrigger1Click="_g_index_query" width="140" />
			</aos:docked>
			<aos:column type="rowno" />
			<aos:selmodel type="checkbox" mode="single" />
			<aos:column header="ID" dataIndex="id" width="30" /> 
			<aos:column header="用户名" dataIndex="name" width="90" />
			<aos:column header="真实姓名" dataIndex="realname" width="90" />
			<aos:column header="角色" dataIndex="actortype" width="90" rendererFn="actor_ren" />
			<aos:column header="所属公司ID" dataIndex="companyid"  width="90" />
			<aos:column header="所属公司" dataIndex="companyname" width="90" />
			<aos:column header="邮箱" dataIndex="email" width="90" />
			<aos:column header="密码" dataIndex="pwd" hidden="false" />
			<aos:column header="状态" dataIndex="state" width="130"
				rendererFn="state_ren" />
			<aos:column header="创建时间" dataIndex="createTime" width="120" />
		</aos:gridpanel>

		<aos:gridpanel id="_g_dic" url="findAllMgrMenu"
			onrender="_g_dic_query" region="center"
			onitemdblclick="_w_dic_u_show" onitemclick="cellclick"
			hidePagebar="true" title="菜单">
			<aos:docked forceBoder="0 0 1 0">
				<aos:dockeditem onclick="_w_dic_show" text="新增" icon="add.png" />
				<aos:dockeditem onclick="_w_dic_u_show" text="修改" icon="edit.png" />
				<aos:dockeditem onclick="_g_dic_del" text="删除" icon="del.png" />
				<aos:dockeditem xtype="tbfill" />
				<aos:triggerfield emptyText="菜单名" id="_code_desc_"
					onenterkey="_g_dic_query" onTrigger1Click="_g_dic_query"
					trigger1Cls="x-form-search-trigger" width="140" />
			</aos:docked>
			<aos:column type="rowno" />
			<aos:selmodel type="checkbox" mode="multi" />
			<aos:column header="是否拥有" dataIndex="id" width="60"
				rendererFn="checkbos" />
			<aos:column header="菜单名" dataIndex="name" width="80" />
			<aos:column header="连接" dataIndex="url" width="120" />
			<aos:column header="图标 " dataIndex="iconCls" width="50" />
			<aos:column header="序号 " dataIndex="sortNo" width="40" />
		</aos:gridpanel>
	</aos:viewport>

	<aos:window id="_w_index" title="新增用户">
		<aos:formpanel id="_f_index" width="400" layout="anchor"
			labelWidth="75">
			<aos:textfield name="name" fieldLabel="用户账号" allowBlank="false" />
			<aos:textfield name="realname" fieldLabel="姓名" allowBlank="false" />
			<aos:textfield name="email" fieldLabel="邮箱" allowBlank="false" />
			<aos:combobox name="actortype" fieldLabel="角色" url="findActor"
				emptyText="－－请选择－－" allowBlank="false" />
			<aos:combobox name="companyid" fieldLabel="所属公司" url="findAgents"
				emptyText="－－请选择－－" allowBlank="false" />
			<aos:textfield name="pwd" fieldLabel="密码" allowBlank="false" />
			<aos:radioboxgroup fieldLabel="状态" columns="[50,50]">
				<aos:radiobox name="state" boxLabel="启用" inputValue="1"
					checked="true" />
				<aos:radiobox name="state" boxLabel="停用" inputValue="2" />
			</aos:radioboxgroup>
		</aos:formpanel>
		<aos:docked dock="bottom" ui="footer">
			<aos:dockeditem xtype="tbfill" />
			<aos:dockeditem onclick="_f_index_submit" text="保存" icon="ok.png" />
			<aos:dockeditem onclick="#_w_index.hide();" text="关闭"
				icon="close.png" />
		</aos:docked>
	</aos:window>

	<aos:window id="_w_index_u" title="修改用户">
		<aos:formpanel id="_f_index_u" width="400" layout="anchor"
			labelWidth="75">
			<aos:textfield name="id" fieldLabel="ID" readOnly="true" />
			<aos:textfield name="name" fieldLabel="用户账号" allowBlank="false" />
			<aos:textfield name="realname" fieldLabel="姓名" allowBlank="false" />
			<aos:textfield name="email" fieldLabel="邮箱" allowBlank="false" />
			<aos:combobox id="_actor_1" name="actortype" fieldLabel="角色" url="findActor"
				emptyText="－－请选择－－" allowBlank="false" />
			<aos:combobox id="_company_1" name="companyid" fieldLabel="所属公司"
				url="findAgents" queryMode="local" emptyText="－－请选择－－"
				allowBlank="false" />
			<aos:textfield name="pwd" fieldLabel="密码" allowBlank="false" />
			<aos:radioboxgroup fieldLabel="状态" columns="[50,50]">
				<aos:radiobox name="state" boxLabel="启用" inputValue="1"
					checked="true" />
				<aos:radiobox name="state" boxLabel="停用" inputValue="2" />
			</aos:radioboxgroup>
		</aos:formpanel>
		<aos:docked dock="bottom" ui="footer">
			<aos:dockeditem xtype="tbfill" />
			<aos:dockeditem onclick="_f_index_u_submit" text="保存" icon="ok.png" />
			<aos:dockeditem onclick="#_w_index_u.hide();" text="关闭"
				icon="close.png" />
		</aos:docked>
	</aos:window>

	<aos:window id="_w_dic" title="新增菜单">
		<aos:formpanel id="_f_dic" width="400" layout="anchor" labelWidth="75">
			<aos:column header="" dataIndex="" />
			<aos:textfield name="name" fieldLabel="菜单名 " />
			<aos:textfield name="url" fieldLabel="连接" />
			<aos:textfield name="iconCls" fieldLabel="图标" />
		</aos:formpanel>
		<aos:docked dock="bottom" ui="footer">
			<aos:dockeditem xtype="tbfill" />
			<aos:dockeditem onclick="_f_dic_submit" text="保存" icon="ok.png" />
			<aos:dockeditem onclick="#_w_dic.hide();" text="关闭" icon="close.png" />
		</aos:docked>
	</aos:window>
	<aos:window id="_w_dic_u" title="修改菜单">
		<aos:formpanel id="_f_dic_u" width="400" layout="anchor"
			labelWidth="75">
			<aos:textfield name="id" fieldLabel="ID" readOnly="true" />
			<aos:textfield name="name" fieldLabel="菜单名 " />
			<aos:textfield name="url" fieldLabel="连接" />
			<aos:textfield name="iconCls" fieldLabel="图标" />
			<aos:textfield name="sortNo" fieldLabel="序号" />
		</aos:formpanel>
		<aos:docked dock="bottom" ui="footer">
			<aos:dockeditem xtype="tbfill" />
			<aos:dockeditem onclick="_f_dic_u_submit" text="保存" icon="ok.png" />
			<aos:dockeditem onclick="#_w_dic_u.hide();" text="关闭"
				icon="close.png" />
		</aos:docked>
	</aos:window>
	<script type="text/javascript">
    function state_ren(v){
		if(v == 1){
			return '启用'
		}else if(v == 2){
			return '停用'
		}else{
			return v
		}
	}
    
    function actor_ren(v){
		if(v == 1){
			return '代理商'
		}else if(v == 2){
			return '渠道'
		}else if(v == 3){
			return '管理员'
		}else if(v ==4){
			return '账务'
		}else if(v == 5){
			return '客服'
		}else{
			return v
		}
	}
	
	//加载完毕执行函数
	window.onload = function() {
		_actor_1_store.load();
		_company_1_store.load();
	}
	

	 
		function cellclick(c1, c2, c3, c4,c5){
			var btn = c5.getTarget('.checkboxButton');
			if (btn) {
				var _record = AOS.selectone(_g_index);
				if (_record && _record.get('id')) {
					 AOS.ajax({
	                        url: 'updateUserMenu',
	                        params: {
		                        	userId: _record.get('id'),
		                        	menuId : btn.name,
		                        	type : btn.checked
	                        },
	                        ok: function (data) {
	                            AOS.tip(data.appmsg);
	                        }
                    });
				} 
			}
		}
		
		function checkbos(value) {
			return '<input name="' + value + '" type="checkbox" class="checkboxButton" style="vertical-align:middle" />';
		}
			 //查询用户
            function _g_index_query() {
                var params = {
                    id: _key_name_.getValue()
                };
                _g_index_store.getProxy().extraParams = params;
                _g_index_store.load();
            }
			 
            //弹出新增用户窗口
            function _w_index_show() {
                AOS.reset(_f_index);
                _w_index.show();
            }

            //弹出修改用户窗口
            function _w_index_u_show() {
                var record = AOS.selectone(_g_index);
                if (record) {
                    _w_index_u.show();
                    _f_index_u.loadRecord(record);
                }
            }

            //删除用户
            function _g_index_del() {
                var selection = AOS.selection(_g_index, 'id');
                if (AOS.empty(selection)) {
                    AOS.tip('删除前请先选中数据。');
                    return;
                }
                var rows = AOS.rows(_g_index);
                var msg = AOS.merge('确认要删除选中的[{0}]条用户吗？', rows);
                AOS.confirm(msg, function (btn) {
                    if (btn === 'cancel') {
                        AOS.tip('删除操作被取消。');
                        return;
                    }
                    AOS.ajax({
                        url: 'delMgrUser',
                        params: {
                            id: selection
                        },
                        ok: function (data) {
                        	_g_index_store.reload();
                            AOS.tip(data.appmsg);
                        }
                    });
                });
            }
            
            //新增
            function _f_index_submit() {
                AOS.ajax({
                    forms: _f_index,
                    url: 'mergeMgrUser',
                    ok: function (data) {
                        if (data.success) {
                            _w_index.hide();
                            _g_index_store.reload();
                        }
                        AOS.tip(data.appmsg);
                    }
                });
            }
            
            //修改
            function _f_index_u_submit() {
                AOS.ajax({
                    forms: _f_index_u,
                    url: 'mergeMgrUser',
                    ok: function (data) {
                        if (data.success) {
                            _w_index_u.hide();
                            _g_index_store.reload();
                        } 
                        AOS.tip(data.appmsg);
                    }
                });
            }
            //---------------------------------------
            
             //查询菜单
            function _g_dic_query() {
                var params = {
                    id: _code_desc_.getValue()
                };
                _g_dic_store.getProxy().extraParams = params;
                _g_dic_store.load();
            }
            
           	function itemclick(){
           		AOS.ajax({
	        			params : {
	        				id : AOS.selectone(_g_index).get('id')
	        			},
                    url: 'findMgrUserAndMenuByMuserId',
                    ok: function (array) {
                    	var btns = Ext.select(".checkboxButton");
        				if (btns && btns.elements.length > 0) {
        					Ext.each(btns.elements, function() {
        						var flag = false;
        						var menuId = this.name;
        						if (array && array.length > 0) {
        							for ( var i in array) {
        								var userAndMenu = array[i];
        								if (userAndMenu.menuId && userAndMenu.menuId == menuId) {
        									flag = true;
        									break;
        								}
        							}
        						}
        						this.checked = flag;
        					})
        				}
                    }
                });
           	}
            
            //弹出新增诊菜单窗口
            function _w_dic_show() {
                AOS.reset(_f_dic);
                _w_dic.show();
            }

            //弹出修改菜单窗口
            function _w_dic_u_show() {
                var record = AOS.selectone(_g_dic);
                if (record) {
                    _w_dic_u.show();
                    _f_dic_u.loadRecord(record);
                }
            }

            
            //新增菜单
            function _f_dic_submit() {
                AOS.ajax({
                    forms: _f_dic,
                    url: 'mergeMgrMenu',
                    ok: function (data) {
                        _w_dic.hide();
                        _g_dic_store.reload();
                        AOS.tip(data.appmsg);
                    }
                });
            }


            //修改菜单
            function _f_dic_u_submit() {
                AOS.ajax({
                    forms: _f_dic_u,
                    url: 'mergeMgrMenu',
                    ok: function (data) {
                        _w_dic_u.hide();
                        _g_dic_store.reload();
                        AOS.tip(data.appmsg);
                    }
                });
            }
            //删除菜单
            function _g_dic_del() {
                var selection = AOS.selection(_g_dic, 'id');
                if (AOS.empty(selection)) {
                    AOS.tip('删除前请先选中数据。');
                    return;
                }
                var rows = AOS.rows(_g_dic);
                var msg = AOS.merge('确认要删除选中的[{0}]条数据菜单吗？', rows);
                AOS.confirm(msg, function (btn) {
                    if (btn === 'cancel') {
                        AOS.tip('删除操作被取消。');
                        return;
                    }
                    AOS.ajax({
                        url: 'delMgrMenu',
                        params: {
                            id: selection
                        },
                        ok: function (data) {
                            AOS.tip(data.appmsg);
                            _g_dic_store.reload();
                        }
                    });
                });
            }
        </script>
</aos:onready>
</aos:html>